// pages/category/category.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        scrollHeight: 667,
        activeIndex: 0,
        // arr:[0,1,2,3,4,5,6,7,8,9],
        categoryItemAll: [
            "上衣",
            "裙子",
            "时尚",
            "食品",
            "家具",
            "潮鞋",
            
          
        ],
        goodsArr:[],
        currentContent:[],
        querenContent:[],
        limianneirong:[],
       
      
           
         


        
        objlist:[],
        objobjlist:[],
        lilineirong:[],
       
   
    },
   changeIndex(options) {
        let id = options.currentTarget.dataset.name;
        console.log(typeof id);
        console.log(this.data.objlist[0]);
        let first=this.data.objlist[ id].map(item=>item.children);
        
        console.log(first);

        this.setData({
            activeIndex: id,
            querenContent:this.data.objlist[id],
            limianneirong: first,
            

        })
        console.log(this.data.querenContent);
    },
    

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        wx.getSystemInfo({
            success: (result) => {
                console.log(result.windowHeight)
                this.setData({
                    scrollHeight: result.windowHeight
                })

            },
        })
        this.getGoodsData();
        console.log(this.data.currentContent);
        console.log(this.data.currentContent);

    },

    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    getGoodsData(){
          let _this=this;
          wx.request({
              url:'https://api-hmugo-web.itheima.net/api/public/v1/categories',
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res){
                  let goodslist=res.data.message;
                  let obj=goodslist.map(item=>{
                      return item.cat_name;
                  })
                  console.log(goodslist);
                  let lists=[];
                  goodslist.forEach(item=>{
                      let {cat_name,cat_icon}=item;
                      let data={
                          name:cat_name,
                          icon:cat_icon,
                      }
                      if(cat_icon){
                          lists.push(data);

                      }
                  })
                 let objlister= goodslist.map(item=>{
                   return item.children;
                  
                })
                let current=objlister[0];
                let limiancurrent=current.map(item=>item.children);
                
               

                  _this.setData({
                      goodsArr:lists,
                      currentContent:obj,
                      objlist:objlister,
                      querenContent:current,
                      limianneirong:limiancurrent,
                      


                  })
                  console.log(_this.data.  objlist);
                  console.log(_this.data.currentContent);
                 



              }
          });
         
    },
    getPic(options){
        let picIndex=this.data.activeIndex;
        console.log(picIndex);
        let index=options.currentTarget.dataset.id;
        console.log(index);
        // let imgSrc=this.data.content[picIndex][id];
        let imgSrc=this.data.content[picIndex][index].pic;
        let goodsName=this.data.content[picIndex][index].name;
        let goodsPrice=this.data.content[picIndex][index].goodsPrice;
        let goodsPriceOld=this.data.content[picIndex][index].goodsPriceOld;
        console.log(imgSrc,goodsName,goodsPrice,goodsPriceOld);
   let arr=this.data.content[picIndex].slice(0,3);
        let firstArr=arr.map(item=>{
         
            return item.pic;
          })
          console.log(firstArr);
          let [firstPic,secondPic,thirdPic]=firstArr;
        let goodsObj={
            goodsPic:imgSrc,
            goodsContent:goodsName,
            goodsContentPrice:goodsPrice,
            goodsContentPriceOld:goodsPriceOld,
            randomPic:[firstPic,secondPic,thirdPic],

        };
        wx.setStorageSync("goodsCartLists",goodsObj);

    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getGoodsData()
        console.log(this.data.currentContent);

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})